<!--
 * @Author: Bennty
 * @Date: 2024-07-21 23:54:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-09-14 15:48:50
 * @Description: 请填写简介
-->
<template>
    <el-dialog v-model="props.visible" title="新增跟进记录" width="30%" @close="handleClose" destory-on-close>
        <el-form :model="formData" ref="formRef" :loading="loading">
            <el-form-item label="上次跟进记录">
                <span>{{ lastData?.contractMessage }}</span>
            </el-form-item>
            <el-form-item label="上次跟进方式">
                <span>{{ props.contract.filter((item:any)=>item.value == lastData?.followType)[0]?.label }}</span>
            </el-form-item>
            <el-form-item label="上次跟进人">
                <span>{{ lastData?.createByName }}</span>
            </el-form-item>
            <el-form-item label="本次跟进方式" prop="contactMethod">
                <el-select v-model="formData.contactMethod">
                    <el-option v-for="item in props.contract" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="下次跟进时间" prop="nextContactDate">
                <el-date-picker type="date" placeholder="下次跟进时间" v-model="formData.nextContactDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="本次跟进记录" prop="contractMessage">
                <el-input type="textarea" maxlength="128" show-word-limit v-model="formData.contractMessage"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" @click="handleSubmit">确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { addFollowRecordApi, getFollowRecordByIdApi } from '@/api/sj/followRecord-crm/index';

const formRef = ref<FormInstance>();
const props = defineProps(['visible','data','contract']);
const emits = defineEmits(['close','submit']);
const loading = ref(false);
const followList = ref<any[]>([]);
const lastData = ref<any>();

const getFollowRecords = async (id: any) => {
    loading.value = true;
    const { data } = await getFollowRecordByIdApi(id);
    followList.value = data;
    lastData.value = data[0];
    loading.value = false;
};

watch(()=>props.data,()=>{
    getFollowRecords(props.data.id);
},{deep: true});

const initForm = reactive({
    contactMethod: undefined,
    contractMessage: undefined,
    followType: 2,
    nextContactDate: undefined
});
const data = reactive({
    formData:{
        ...initForm
    }
});
const { formData } = toRefs(data);

console.log(props.data);

const handleSubmit = async () => {
    const submitData = {
        ...formData.value,
        customerName: props.data.customerName,
        customerId: props.data.id,
        imageIds: 'image'
    };
    await addFollowRecordApi(submitData).then((result)=>{
        if(result.code == 200){
            emits('submit',true);
        }else{
            emits('submit',false);
        }
    });
};

const handleClose = ()=>{
    emits('close');
};
</script>

<style scoped>
.dialog-footer{
    display:flex;
    justify-content: center;
}
</style>